package com.wyx.bbs.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Component
public class OSSClientUtil {

protected static final Logger log = LoggerFactory.getLogger(OSSClientUtil.class);

//阿里云OSS地址，这里看根据你的oss选择
protected static String endpoint = "oss-cn-shenzhen.aliyuncs.com";
//阿里云OSS账号
protected static String accessKeyId  = "LTAI5tJm6bFukwjGuHeSpUWp";
//阿里云OSS密钥
protected static String accessKeySecret  = "W4shyHo588mr9mWQvVXElL4V0kJhSh";
//阿里云OSS上的存储块bucket名字
protected static String bucketName  = "wyx-bbs-file";

//阿里云图片文件存储目录
private String homeimagedir = "somnus/";


//文件存储目录
private String filedir = "APP/";

//public String uploadImage(MultipartFile file){
//    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//// 获取文件名
//    String fileName = file.getOriginalFilename();
//// 获取文件后缀名
//    String suffixName = fileName.substring(fileName.lastIndexOf("."));
//// 最后上传生成的文件名
//    String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
//// oss中的文件夹名
////    String objectName = sdf.format(new Date()) + "/" + finalFileName;
//    String objectName = homeimagedir + finalFileName;
//// 创建上传文件的元信息，可以通过文件元信息设置HTTP header(设置了才能通过返回的链接直接访问)。
//    ObjectMetadata objectMetadata = new ObjectMetadata();
//    objectMetadata.setContentType("image/jpg");
////    objectMetadata.setContentDisposition("attachment;");
//// 文件上传
//    try {
//        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(file.getBytes()),objectMetadata);
//    } catch (IOException e) {
//        e.printStackTrace();
//    }
//// 设置URL过期时间为1小时。
//    Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000 * 24 * 10);
//    String url = ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString();
//    ossClient.shutdown();
//    System.out.println("阿里云OSS的文件url:{}"+ url);
//    return url;
//}

/**
 *
 * 上传图片
 * @param file
 * @return
 */
public String uploadImg2Oss(MultipartFile file) {
    if (file.getSize() > 1024 * 1024 *20) {
        return "图片太大";//RestResultGenerator.createErrorResult(ResponseEnum.PHOTO_TOO_MAX);
    }
    String originalFilename = file.getOriginalFilename();
    String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
    Random random = new Random();
    String name = random.nextInt(10000) + System.currentTimeMillis() + substring;
    try {
        InputStream inputStream = file.getInputStream();
        this.uploadFile2OSS(inputStream, name);
        return name;//RestResultGenerator.createSuccessResult(name);
    } catch (Exception e) {
        return "上传失败";//RestResultGenerator.createErrorResult(ResponseEnum.PHOTO_UPLOAD);
    }
}

/**
 * 上传图片获取fileUrl
 * @param instream
 * @param fileName
 * @return
 */
private String uploadFile2OSS(InputStream instream, String fileName) {
    String ret = "";
    try {
        //创建上传Object的Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(instream.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //上传文件

        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        PutObjectResult putResult = ossClient.putObject(bucketName, filedir + fileName, instream, objectMetadata);
        ret = putResult.getETag();
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            if (instream != null) {
                instream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return ret;
}

public static String getcontentType(String FilenameExtension) {
    if (FilenameExtension.equalsIgnoreCase(".bmp")) {
        return "image/bmp";
    }
    if (FilenameExtension.equalsIgnoreCase(".gif")) {
        return "image/gif";
    }
    if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
            FilenameExtension.equalsIgnoreCase(".jpg") ||
            FilenameExtension.equalsIgnoreCase(".png")) {
        return "image/jpeg";
    }
    if (FilenameExtension.equalsIgnoreCase(".html")) {
        return "text/html";
    }
    if (FilenameExtension.equalsIgnoreCase(".txt")) {
        return "text/plain";
    }
    if (FilenameExtension.equalsIgnoreCase(".vsd")) {
        return "application/vnd.visio";
    }
    if (FilenameExtension.equalsIgnoreCase(".pptx") ||
            FilenameExtension.equalsIgnoreCase(".ppt")) {
        return "application/vnd.ms-powerpoint";
    }
    if (FilenameExtension.equalsIgnoreCase(".docx") ||
            FilenameExtension.equalsIgnoreCase(".doc")) {
        return "application/msword";
    }
    if (FilenameExtension.equalsIgnoreCase(".xml")) {
        return "text/xml";
    }
    return "image/jpeg";
}

/**
 * 获取图片路径
 * @param fileUrl
 * @return
 */
public String getImgUrl(String fileUrl) {
    if (!StringUtils.isEmpty(fileUrl)) {
        String[] split = fileUrl.split("/");
        String url =  this.getUrl(this.filedir + split[split.length - 1]);
//                log.info(url);
//                String[] spilt1 = url.split("\\?");
//                return spilt1[0];
        return url;
    }
    return null;
}

/**
 * 获得url链接
 *
 * @param key
 * @return
 */
public String getUrl(String key) {
    // 设置URL过期时间为10年  3600l* 1000*24*365*10
    Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
    // 生成URL
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
    if (url != null) {
        return url.toString();
    }
    return null;
}


/**
 * 多图片上传
 * @param fileList
 * @return
 */
public String checkList(List<MultipartFile> fileList) {
    String  fileUrl = "";
    String  str = "";
    String  photoUrl = "";
    for(int i = 0;i< fileList.size();i++){
        fileUrl = uploadImg2Oss(fileList.get(i));
        str = getImgUrl(fileUrl);
        if(i == 0){
            photoUrl = str;
        }else {
            photoUrl += "," + str;
        }
    }
    return photoUrl.trim();
}

/**
 * 单个图片上传
 * @param file
 * @return
 */
public String checkImage(MultipartFile file){
    String fileUrl = uploadImg2Oss(file);
    String str = getImgUrl(fileUrl);
    return str.trim();
}
}

